﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.ViewModel;

namespace WMSSoft.WinKStart.ViewModel
{
    public class CModulesDataSourceViewModel : SettingBaseListViewModel
    {
        List<DataSource> _Data = null;
        DataSource _SelectedItem = null;
        System.Windows.Controls.ListView _tmpListView = null;
        bool _DataSourceSelected = false;

        public CModulesDataSourceViewModel() { }

        public CModulesDataSourceViewModel(List<DataSource> Data)
        {
            _Data = Data;
            PropertyChanged += CModulesDataSourceViewModel_PropertyChanged;

            ViewModel_AddClick += CModulesDataSourceViewModel_ViewModel_AddClick;
            ViewModel_EditClick += CModulesDataSourceViewModel_ViewModel_EditClick;
            ViewModel_DeleteClick += CModulesDataSourceViewModel_ViewModel_DeleteClick;

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("View Data"), new CDelegatingCommand(Delegate => this.Cmd_Exec_ViewData())));
        }

        void CModulesDataSourceViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "SelectedItem")
                {
                    if (SelectedItem == null)
                        DataSourceSelected = false;
                    else
                        DataSourceSelected = true;
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on Property changing!", Ex, true, EMessageTypes.Error);
            }
        }

        void CModulesDataSourceViewModel_ViewModel_DeleteClick(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteClick.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteClick.Command).CommandParameter;
                    if (SelectedItem != null)
                    {
                        if (System.Windows.MessageBox.Show("Delete selected DataSource?", "Question",
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                        {
                            Data.Remove(SelectedItem);

                            if (tmpListView != null) tmpListView.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {

                CMessagelogging.AppendMessage("Error on deleting Data!", Ex, true, EMessageTypes.Error);
            }
        }

        void CModulesDataSourceViewModel_ViewModel_EditClick(object sender, EventArgs e)
        {
            try
            {
                if (SelectedItem != null)
                {                    
                    if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditClick.Command).CommandParameter != null)
                    {
                        _tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditClick.Command).CommandParameter;
                        Settings_NewEditDataSourceViewModel tmpViewModel = new Settings_NewEditDataSourceViewModel(SelectedItem);
                        tmpViewModel.DataChanged += tmpViewModel_DataChanged;
                        Settings_NewEditDataSource tmpDialog = new Settings_NewEditDataSource();
                        tmpDialog.DataContext = tmpViewModel;
                        if (tmpDialog.ShowDialog() == true)
                        {
                            SelectedItem = ((Settings_NewEditDataSourceViewModel)tmpDialog.DataContext).Data; 
                            if (_tmpListView != null) _tmpListView.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on editing DataSource!", Ex, true, EMessageTypes.Error);
            }
        }

        void tmpViewModel_DataChanged(object sender, EventArgs e)
        {
            try
            {
                if (SelectedItem != null)
                    SelectedItem = ((Settings_NewEditDataSourceViewModel)sender).Data;
                else
                {
                    if (FindDataSource(((Settings_NewEditDataSourceViewModel)sender).Data) == false)
                    {
                        Data.Add(((Settings_NewEditDataSourceViewModel)sender).Data);

                        SelectedItem = Data[Data.Count - 1];
                    } else
                        System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_MODULES_DATASOURCE_ADD_DATASOURCE_ALREADY_EXISTS, EStringTypes.stMessage),
                                                       LanguageStrings.GetText("INFO"), System.Windows.MessageBoxButton.OK);                    
                }
                if (_tmpListView != null) _tmpListView.Items.Refresh();
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on applying Data!", Ex, true, EMessageTypes.Error);                
            }
        }

        void CModulesDataSourceViewModel_ViewModel_AddClick(object sender, EventArgs e)
        {
            try
            {
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddClick.Command).CommandParameter != null)
                {
                    _tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddClick.Command).CommandParameter;
                    Settings_NewEditDataSourceViewModel tmpViewModel = new Settings_NewEditDataSourceViewModel();
                    tmpViewModel.DataChanged += tmpViewModel_DataChanged;
                    SelectedItem = null;
                    Settings_NewEditDataSource tmpDialog = new Settings_NewEditDataSource();
                    tmpDialog.DataContext = tmpViewModel;
                    if (tmpDialog.ShowDialog() == true)
                    {
                        if (SelectedItem == null)
                        {
                            if (FindDataSource(((Settings_NewEditDataSourceViewModel)tmpDialog.DataContext).Data) == false)
                                Data.Add(((Settings_NewEditDataSourceViewModel)tmpDialog.DataContext).Data);
                            else
                                System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_MODULES_DATASOURCE_ADD_DATASOURCE_ALREADY_EXISTS, EStringTypes.stMessage), 
                                                               LanguageStrings.GetText("INFO"), System.Windows.MessageBoxButton.OK);
                        }
                        else
                        {
                            SelectedItem = ((Settings_NewEditDataSourceViewModel)tmpDialog.DataContext).Data;
                        }
                        if (_tmpListView != null) _tmpListView.Items.Refresh();
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on adding DataSource!", Ex, true, EMessageTypes.Error);
            }
        }

        void Cmd_Exec_ViewData() 
        {
            try
            {
                if (SelectedItem != null)
                {
                    Settings_ViewDataSourceData tmpViewDataSource = new Settings_ViewDataSourceData(SelectedItem);
                    tmpViewDataSource.Show();
                }
            }
            catch (Exception Ex)
            {
                //TODO: implements error handler
            }
        }

        #region Properties

        public CModulesDataSourceViewModel(string ViewName)
        {
            base.ViewName = ViewName;
        }

        public List<DataSource> Data
        {
            get { return _Data; }
        }

        public DataSource SelectedItem
        {
            get { return _SelectedItem; }
            set
            {
                _SelectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        public bool DataSourceSelected
        {
            get { return _DataSourceSelected; }
            set
            {
                _DataSourceSelected = value;
                OnPropertyChanged("DataSourceSelected");
            }
        }

        public CSingleCommandViewModel Cmd_ViewDataClick
        {
            get { return m_oCommands[6]; }
        }

        #endregion
        bool FindDataSource(DataSource Item)
        {
            try
            {
                var listResult = from DSItem in Data where DSItem.Name.ToUpper() == Item.Name.ToUpper() select DSItem;

                if (listResult != null && listResult.Count() > 0)
                    return true;
                else 
                    return false;
            }
            catch (Exception Ex)
            {                
                throw  new Exception("Error on search DataSource-Item!", Ex);
            }
        }

        //Settings_NewEditDataSource
    }
}
